<?PHP
  class WYSIWYG extends Editor{
  
    public function display(){
	  include(filterfilename("content/articles/".$this->page->alias));
	}

    function getHeader(){
    }
	
	public function getEditableCode(){
		if($this->content == null){
			$this->content = $this->readContent("../");
		}
		$template = new Template();
		$template->load("control_wysiwyg");
		$template->assign_var("CONTENT",$this->content);
		$template->assign_var("ALIAS",$this->page->alias);
		$template->assign_var("URL",$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
		return $template->getCode();
	}
	
	public function save($newPage,$oldPage){
		$this->deleteContent("../");
		$this->page = $newPage;
		$this->writeContent("../",$_POST['content']);
	}
	
	private function readContent($dirpraefix){
		$filename = $dirpraefix."content/articles/".$this->page->alias.".php";
		$handle = @fopen ($filename, "rb");
		if($handle && filesize($filename) > 0){
			$contents = fread ($handle, filesize ($filename));
			fclose ($handle);
		}
		else{
			$contents = "";
		}
		return $contents;
	}
	
	private function deleteContent($dirpraefix){
		$filename = $dirpraefix."content/articles/".$this->page->alias.".php";
		if(file_exists($filename)){
			unlink($filename);
			$args['alias'] = $this->page->alias;
			$args['filename']  = $filename;
			EventManager::raiseEvent("pagecontent_deleted","../",$args);
		}
	}
	
	function writeContent($dirpraefix,$content){
		$filename = $dirpraefix."content/articles/".$this->page->alias.".php";
		$handle = fopen ($filename, "a");
		$res = fwrite ($handle, $content);
		fclose ($handle);
		if($res){
			$args['alias'] = $alias;
			$args['filename']  = $filename;
			$args['content'] = $content;
			EventManager::raiseEvent("pagecontent_writed","../",$args);
		}
		return $res;
	}
  
  }
?>